<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->view('welcome_message');
	}

	public function file()
	{
		$this->load->view('file', array('error' => ' ' ));
	}

	public function do_upload()
    {	
    	$this->getFilePatch('xax');
    	$this->getFilePatch('xax/asdas/a');
    	$this->getFilePatch('xas/asdas/a');
    	$this->getFilePatch('xas/s/a');
        $config['upload_path']      = $this->getFilePatch('xa');
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']     = 888888;
        $config['max_width']        = 10240;
        $config['max_height']       = 7680;

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('userfile'))
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('file', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
    }

    public function getFilePatch($r)
    {	
    	// $this->rm_empty_dir('./uploads');
		$config['upload_path'] = './uploads/images/' . $r . '/' . date('Y-m-d') . '/';

		if (!is_dir($config['upload_path'])) {
			mkdir($config['upload_path'], '0777', true);
		}

		return $config['upload_path'];

    }

    /** 删除所有空目录 
    * @param String $path 目录路径 
    */
    public function rm_empty_dir($path) {
    	if (function_exists('rmdir')) {
    		// 判断路径是否是目录，切能打开
	  		if (is_dir($path) && ($handle = opendir($path)) !== false) {
	  			// 循环遍历目录下的文件
	  			while(($file = readdir($handle)) !== false) {
	  				// 当目录不是 . 或 .. 时继续执行
	  				if ($file != '.' && $file != '..') {
	  					// 拼接当前路径
	  					$curfile = $path . '/' . $file;
	  					// 判断当前路径是否是目录
	  					if (is_dir($curfile)) {
	  						// 是目录则继续遍历
	  						$this->rm_empty_dir($curfile);
	  						// 删除空目录（=2是因为.和..存在）
	  						if (count(scandir($curfile)) == 2) {
	  							rmdir($curfile);
	  						}
	  					}
	  				}
	  			}
	  		}
    	}
    }
}
